//
//  DWDatabaseManager.m
//  Alamofire
//
//  Created by OS on 2022/10/18.
//

#import "DWDatabaseManager.h"
#import "FMDB.h"
#import "FCFileManager.h"

@interface DWDatabaseManager()

@property (nonatomic, strong) FMDatabase *db;

@end

@implementation DWDatabaseManager

+ (instancetype)sharedManager {
    static id instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
}

- (FMDatabase *)db {
    if (_db == nil) {
        NSString *path = [FCFileManager pathForCachesDirectoryWithPath:@"test.db"];
        _db = [FMDatabase databaseWithPath:path];
    }
    return _db;
}

- (BOOL)createTable {
    
    if (![self.db open]) {
        self.db = nil;
        return NO;
    }
    
    NSString *commend = @"CREATE TABLE IF NOT EXISTS Feedback (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,name TEXT,age INTEGER);";
    
    BOOL res = [self.db executeUpdate:commend];
    
    [self.db close];
    
    return res;
}

- (BOOL)insert {
    
    if (![self.db open]) {
        self.db = nil;
        return NO;
    }
    
    NSString *commend = @"INSERT INTO Feedback (name,age) VALUES ('百度','4'),('腾讯','2'),('字节','8');";
    
    BOOL res = [self.db executeUpdate:commend];
    
    [self.db close];
    
    return res;
    
}

- (BOOL)update {
    
    if (![self.db open]) {
        self.db = nil;
        return NO;
    }
    
    NSString *commend = @"UPDATE Feedback SET age='10' WHERE id='1';";
    
    BOOL res = [self.db executeUpdate:commend];
    
    [self.db close];
    
    return res;
}



@end
